package me.spring.cloud.common.components.monitor.util;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Transaction;

/**
 * @description: cat业务计数
 * @author: luffy
 * @create: 2018-12-03 11:19
 **/
public class MonitorUtil {

  private final static String businessKey = "BusinessKey";

  public static void count(String businessName) {
    count(businessName, 1);
  }

  public static void count(String businessName, int i) {
    Transaction tx = Cat.newTransaction(businessKey, businessName);
    try {
      if (null != tx) {
        Cat.logMetricForCount(businessName, i);
        tx.setSuccessStatus();
      }
    } catch (Throwable e) {
      if (null != tx) {
        tx.setStatus(e);
        Cat.logError(e);
        throw e;
      }
    } finally {
      if (null != tx) {
        tx.complete();
      }
    }
  }

  public static void sum(String businessName, double i) {
    Transaction tx = Cat.newTransaction(businessKey, businessName);
    try {
      if (null != tx) {
        Cat.logMetricForSum(businessName, i);
        tx.setSuccessStatus();
      }
    } catch (Throwable e) {
      if (null != tx) {
        tx.setStatus(e);
        Cat.logError(e);
        throw e;
      }
    } finally {
      if (null != tx) {
        tx.complete();
      }
    }
  }
}
